package com.shark.springbootcms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shark.springbootcms.common.Result;
import com.shark.springbootcms.common.ResultCode;
import com.shark.springbootcms.form.PermsForm;
import com.shark.springbootcms.form.UserForm;
import com.shark.springbootcms.mapper.MenuMapper;
import com.shark.springbootcms.mapper.RoleMapper;
import com.shark.springbootcms.model.Role;
import com.shark.springbootcms.service.RoleService;
import com.shark.springbootcms.utils.IdWorker;
import com.shark.springbootcms.vo.MenuVo;
import com.shark.springbootcms.vo.PermsVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class RoleServiceImpl implements RoleService {
    @Autowired
    private IdWorker idWorker;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public Result addRole(Role form) {
        form.setId(String.valueOf(idWorker.nextId()));
        roleMapper.addRole(form);
        Result success = Result.SUCCESS();
        return success;
    }

    @Override
    public Result list(UserForm form) {
        PageHelper.startPage(form.getPage(), form.getPageSize());
        List<Role> roles = roleMapper.roleList(form);
        PageInfo<Role> info = new PageInfo<>(roles);
        return new Result(ResultCode.SUCCESS, info);
    }

    @Override
    public Result edit(Role role, String id) {
        role.setId(id);
        roleMapper.editRole(role);
        Result success = Result.SUCCESS();
        return success;
    }

    @Override
    public Result delete(String id) {
        //删除角色
        roleMapper.deleteRoleById(id);
        //删除角色菜单
        roleMapper.deleteRoleMenu(id);
        //删除用户角色
        roleMapper.deleteUserRole(id);
        Result success = Result.SUCCESS();
        return success;
    }

    /**
     * 权限
     *
     * @return
     */
    @Override
    public Result perms(String roleId) {
        List<MenuVo> list = menuMapper.selectAll();
        List<MenuVo> result = new ArrayList<>();
        List<MenuVo> nodes = new ArrayList<>();
        List<MenuVo> leaf = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            for (MenuVo vo : list) {
                if (StringUtils.isBlank(vo.getParentId())) {
                    result.add(vo);
                } else if (StringUtils.equalsIgnoreCase("1", vo.getType())) {
                    nodes.add(vo);
                } else {
                    leaf.add(vo);
                }
            }
        }
        List<PermsVo> two = new ArrayList<>();
        if (!CollectionUtils.isEmpty(nodes)) {
            for (MenuVo vo : nodes) {
                PermsVo top = new PermsVo();
                top.setId(vo.getId());
                top.setLabel(vo.getName());
                top.setParentId(vo.getParentId());
                List<PermsVo> vos = new ArrayList<>();
                if (!CollectionUtils.isEmpty(leaf)) {
                    for (MenuVo menuVo : leaf) {
                        if (menuVo.getParentId().equalsIgnoreCase(vo.getId())) {
                            PermsVo permsVo = new PermsVo();
                            permsVo.setId(menuVo.getId());
                            permsVo.setLabel(menuVo.getName());
                            permsVo.setParentId(menuVo.getParentId());
                            vos.add(permsVo);
                        }
                    }
                }
                if (CollectionUtils.isEmpty(vos)) {
                    top.setChildren(null);
                } else {
                    top.setChildren(vos);
                }
                two.add(top);
            }
        }
        List<PermsVo> one = new ArrayList<>();
        if (!CollectionUtils.isEmpty(result)) {
            for (MenuVo vo : result) {
                PermsVo top = new PermsVo();
                top.setId(vo.getId());
                top.setLabel(vo.getName());
                List<PermsVo> vos = new ArrayList<>();
                if (!CollectionUtils.isEmpty(two)) {
                    for (PermsVo menuVo : two) {
                        if (menuVo.getParentId().equalsIgnoreCase(vo.getId())) {
                            vos.add(menuVo);
                        }
                    }
                }

                if (!CollectionUtils.isEmpty(vos)) {
                    top.setChildren(vos);
                } else {
                    top.setChildren(null);
                }
                one.add(top);

            }
        }
        //查询该角色对应的菜单权限ID
        List<String> ids = roleMapper.selectRoleMenu(roleId);
        Map map = new HashMap<>();
        map.put("menu", one);
        map.put("perms", ids);
        return Result.SUCCESS(map);

    }

    @Override
    public Result addPerms(PermsForm form) {
        //先删除角色菜单中间表数据
        roleMapper.deleteRoleMenuById(form.getRoleId());
        //在建立新的关系
        roleMapper.insertRoleMenu(form.getRoleId(), form.getIds());
        return Result.SUCCESS();
    }
}
